﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Gp.GCX;
using Gp.Scripts.Core;
using MoreMountains.Feedbacks;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.GUI.Reaction {
    public class ReactionWindowController : MonoBehaviour, IController {
        public GameObject window;
        public ReactionUIButton uiButtonPrefab;

        private readonly List<ReactionUIButton> _buttons = new();
        [SerializeField] private ReactionUIButton cancelButton;

        private bool _waitForInput = false;


        [SerializeField] [Range(0, 1)]private float startValue = .5f;
        [SerializeField] [Range(0, .4f)]private float delay;
        [SerializeField] private float startDelay;
        
        [SerializeField] private Ease ease = Ease.InOutCirc;
        
        
        

        private void Start() {
            // if (Global.Get<GridBattleGUISystem>().ReactionWindowController == null)
                // Global.Get<GridBattleGUISystem>().ReactionWindowController = this;

            cancelButton.button.onClick.AddListener(ClearSelection);
            this.RegisterEvent<Event_BattleEnd>(_ => ClearSelection());
        }


        public IEnumerator ResolveReactionSelection<T>(List<BaseReactionEntity> wrappers, T args)
            where T : ReactionArgs {
            // 阻碍输入
            Global.Get<BattleSystem>().BlockInput(this);

            // 激活窗口
            window.SetActive(true);
            _waitForInput = true;

            int count = 0;

            // ============================= 选项渲染 ========================================
            foreach (var item in wrappers) {
                // 如果不满足条件，不触发
                if (!item.CanReaction(args)) {
                    continue;
                }

                // 渲染选项
                // RenderSelection(item, args);
                // count++;
            }

            if (count == 0) {
                ClearSelection();
                yield break;
            }

            // 将取消按钮至于末尾
            cancelButton.transform.SetAsLastSibling();

            yield return new WaitForSeconds(startDelay);
            var tweener = DOTween.To(x => MMTimeManager.Instance.SetTimeScaleTo(x), startValue, 0, delay).SetEase(ease);

            while (_waitForInput) {
                yield return null;
            }
            
            tweener.Kill();
            MMTimeManager.Instance.SetTimeScaleTo(MMTimeManager.Instance.NormalTimeScale);
            // 等待一帧
            yield return null;
        }


        // private void RenderSelection<T>(ReactionEventWrapper<T> wrapper, T args) where T : ReactionArgs {
        //     var obj = uiButtonPrefab.Instantiate();
        //     obj.transform.SetParent(window.transform, false);
        //     _buttons.Add(obj);
        //     obj.RenderSelection(wrapper, this, args);
        // }


        private void ClearSelection() {
            ClearSelection(true);
        }


        private void ClearSelection(bool changeWaitForInputFlag) {
            window.SetActive(false);
            // MMTimeManager.Instance.SetTimeScaleTo(1);
            if (changeWaitForInputFlag) _waitForInput = false;
            Global.Get<BattleSystem>().UnBlockInput(this);


            foreach (var button in _buttons) {
                Destroy(button.gameObject);
            }

            _buttons.Clear();
        }


        public void OnSelect<T>(ReactionArgs wrapper, T args) where T : ReactionArgs {
            StartCoroutine(Process());
            
            IEnumerator Process() {
                yield return null;
                yield return null;
                // args.CallBack = () => wrapper.OnTrigger(args);
                // todo： 技能目标选择
                
                
                ClearSelection(false);

                // yield return wrapper.OnSelect?.Invoke(args);
                
                _waitForInput = false;
            }
        }

        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}